/**
* Authorize.Net Edit settings Page
*
* This page will render the GUI and the AuthorizeNet settings feilds. This page is
* commented extensively as the authorizenet code is used to base many payment modules
*
* @access 	public
* @author 	Rob
* @copyright	ResMania 2009 all rights reserved.
* @version	1.0
* @link		http://developer.resmania.com/docs:creating_resmania_plugins
* @since  	05-2009
*/

/**
* RM.Pages.Plugin_AuthorizeNet_Config_Toolbar
*
* Setup the AuthorizeNet Edit Page toolbar Buttons. This function will render the toolbar
* to the upper right of the GUI. For this page we just need one button to save settings.
*
* @return   ExtJS Panel
*/
RM.Pages.Plugin_AuthorizeNet_Config_Toolbar = {
    xtype : "panel",
    id : "rm_pages_plugins_authorizenet_toolbar",
    bodyBorder : false,
    html : RM.Common.getToolbar([{image: RM.BaseLargeImageURL+"save.gif", label: RM.Translate.Admin.AuthorizeNet.Main.Save, link: "RM.Pages.Functions.plugins_authorizenet_save()"}])
}
RM.Toolbars.push(RM.Pages.Plugin_AuthorizeNet_Config_Toolbar);

/**
* RM.Pages.Functions.plugins_authorizenet_save
*
*/
RM.Pages.Functions.plugins_authorizenet_save = function(){
    RM.Pages.Plugin_AuthorizeNet_Panel.getForm().submit({
        params: {
            'id' : Ext.getDom('rm_pages_plugins_authorizenet_id').value,
            'api_id' : Ext.getDom('rm_pages_plugins_authorizenet_api_id').value,
            'trans_key' : Ext.getDom('rm_pages_plugins_authorizenet_transkey').value,
            'pre_authorize' : Ext.getDom('rm_pages_plugins_authorizenet[pre_authorize]').value,
            'test_mode' : Ext.getDom('rm_pages_plugins_authorizenet[test_mode]').value,
            'default' : Ext.getDom('rm_pages_plugins_authorizenet[default]').value
        },
        success: function(form, action) {
            RM.Pages.AuthorizeNet_StatusBar = Ext.getCmp('rm_pages_plugins_authorizenet_statusbar');
            RM.Pages.AuthorizeNet_StatusBar.setStatus({
                text: RM.Translate.Admin.AuthorizeNet.Main.SaveOK,
                iconCls: 'ok-icon'
            });
        },
        failure: function() {
            RM.Pages.AuthorizeNet_StatusBar = Ext.getCmp('rm_pages_plugins_authorizenet_statusbar');
            RM.Pages.AuthorizeNet_StatusBar.setStatus({
                text: RM.Translate.Admin.AuthorizeNet.Main.SaveFailed,
                iconCls: 'failed-icon'
            });
        },
        waitMsg: RM.Translate.Common.Saving,
        waitTitle: RM.Translate.Common.PleaseWait
    });
}


// the hidden ID feild use to connect this to the DB record.
RM.Pages.Plugin_AuthorizeNet_ID_Field = new Ext.form.Hidden({
    id : "rm_pages_plugins_authorizenet_id"
});

// the api id feild
RM.Pages.Plugin_AuthorizeNet_ApiID_Field = new Ext.form.TextField({
    fieldLabel: RM.Translate.Admin.AuthorizeNet.Main.APIid,
    id : "rm_pages_plugins_authorizenet_api_id",
    width : 200
});

// the trans key feild
RM.Pages.Plugin_AuthorizeNet_TransKey_Field = new Ext.form.TextField({
    fieldLabel: RM.Translate.Admin.AuthorizeNet.Main.TransKey,
    id : "rm_pages_plugins_authorizenet_transkey",
    width : 200
});

// the pre-authorize payments account checkbox
RM.Pages.Plugin_AuthorizeNet_PreAuthorize_Selection = {
    id: "rm_pages_plugins_authorizenet[pre_authorize]",
    name: "rm_pages_plugins_authorizenet_pre_authorize",
    inputValue: "1",
    boxLabel: RM.Translate.Admin.AuthorizeNet.Main.PreAuthorize,
    xtype: "xcheckbox"
};

// the pre-authorize payments account checkbox
RM.Pages.Plugin_AuthorizeNet_TestMode_Selection = {
    id: "rm_pages_plugins_authorizenet[test_mode]",
    name: "rm_pages_plugins_authorizenet_test_mode",
    inputValue: "1",
    boxLabel: RM.Translate.Admin.AuthorizeNet.Main.TestMode,
    xtype: "xcheckbox"
};

// default checkbox
RM.Pages.Plugin_AuthorizeNet_Default_Selection = {
    id: "rm_pages_plugins_authorizenet[default]",
    name: "rm_pages_plugins_authorizenet_default",
    inputValue: "1",
    boxLabel: RM.Translate.Admin.AuthorizeNet.Main.Default,
    xtype: "xcheckbox"
};

// get account message
RM.Pages.Plugin_AuthorizeNet_GetAccountText = new Ext.Panel({
    fieldLabel: '',
    html: '<div id="rm_pages_admin_form_textlink"><a href="https://www.authorizenet.com/" target="_blank">'+RM.Translate.Admin.AuthorizeNet.Main.GetAccount+'</a></div>',
    bodyBorder : false
});


// feildset
RM.Pages.Plugin_AuthorizeNet_Fieldset = new Ext.form.FieldSet({
    id: "rm_pages_plugins_authorizenet_fieldset",
    title: RM.Translate.Admin.AuthorizeNet.Main.Settings,
    autoHeight: true,
    layout: 'form',
    bodyBorder : false,
    items: [
        RM.Pages.Plugin_AuthorizeNet_ID_Field,
        RM.Pages.Plugin_AuthorizeNet_ApiID_Field,
        RM.Pages.Plugin_AuthorizeNet_TransKey_Field,
        RM.Pages.Plugin_AuthorizeNet_PreAuthorize_Selection,
        RM.Pages.Plugin_AuthorizeNet_TestMode_Selection,
        RM.Pages.Plugin_AuthorizeNet_GetAccountText,
        RM.Pages.Plugin_AuthorizeNet_Default_Selection
    ]
});


RM.Pages.Functions.AuthorizeNet_ConfigJson = function (responseObject) {

    var myMask = new Ext.LoadMask('rm_pages_plugins_authorizenet_main_form', {
        msg: RM.Translate.Common.PleaseWait,
        msgCls: "RM_Loading_Mask_Msg_Hidden" // hide the msg
    });
    myMask.show();

    var settings = responseObject.settings[0];

    // set the values of the form items
    Ext.getCmp("rm_pages_plugins_authorizenet_id").setValue(settings.id);
    Ext.getCmp("rm_pages_plugins_authorizenet_api_id").setValue(settings.api_id);
    Ext.getCmp("rm_pages_plugins_authorizenet_transkey").setValue(settings.trans_key);
    Ext.getCmp("rm_pages_plugins_authorizenet[pre_authorize]").setValue(settings.pre_authorize);
    Ext.getCmp("rm_pages_plugins_authorizenet[test_mode]").setValue(settings.test_mode);
    Ext.getCmp("rm_pages_plugins_authorizenet[default]").setValue(settings.defaultplugin);

    Ext.getCmp('rm_pages_plugins_authorizenet_main_form').render();
    Ext.getCmp('rm_pages_plugins_authorizenet_main_form').doLayout();

    Ext.getCmp('toolbar_panel').layout.setActiveItem('rm_pages_plugins_authorizenet_toolbar');
    Ext.getCmp('content-panel').layout.setActiveItem('rm_pages_plugins_authorizenet_main_form');

    myMask.hide();

    RM.Help.Load('Admin.AuthorizeNet.Edit.Main');

}

// main panel
RM.Pages.Plugin_AuthorizeNet_Panel = new Ext.FormPanel({
    id : 'rm_pages_plugins_authorizenet_main_form',
    title: RM.Translate.Admin.AuthorizeNet.Main.Title,
    bodyStyle : "padding:10px;",
    items : [
        RM.Pages.Plugin_AuthorizeNet_Fieldset
    ],
    url : RM.Common.AssembleURL({
        controller: 'AuthorizeNet',
        action: 'updateJson'
    }),
    bbar: new Ext.ux.StatusBar({
        id: 'rm_pages_plugins_authorizenet_statusbar',
        items: []
    })
});

RM.Main.Pages.push(RM.Pages.Plugin_AuthorizeNet_Panel);